﻿using SS.Common.Utilities;
using SS.WPF.Framework.Localization.Entity;

namespace SS.WPF.Framework.Localization
{
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Globalization;
    using System.Linq;
    using System.Threading;

    /// <summary>
    /// The class is used to translate message defined in XML file
    /// </summary>
    public class XmlTranslationProvider : ITranslationProvider
    {
        #region Fields

        /// <summary>
        /// The distionary that is used to store messages of all supported language
        /// Key: language code
        /// Value: messages dictionary
        /// </summary>
        private readonly Dictionary<string, StringDictionary> _languageDictionary =
            new Dictionary<string, StringDictionary>();

        #endregion

        #region Property

        private readonly string _datafile;
        /// <summary>
        /// File contains language messages
        /// </summary>
        public string Datafile
        {
            get
            {
                return this._datafile;
            }
        }

        private IEnumerable<CultureInfo> _languages;
        /// <summary>
        /// Gets the available languages.
        /// </summary>
        /// <value>The available languages.</value>
        public IEnumerable<CultureInfo> Languages
        {
            get
            {
                return this._languages;
            }
        }

        private static string CurrentLanguageCode
        {
            get
            {
                return Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName;
            }
        }

        #endregion

        #region Ctors

        /// <summary>
        /// Create an instane of XmlTranslationProvider with specified Data file
        /// </summary>
        /// <param name="datafile">File contains language messages</param>
        public XmlTranslationProvider(string datafile)
        {
            this._datafile = datafile;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Initialize XML language translator
        /// </summary>
        public void Initialize()
        {
            // Create language model
            var languageConfiguration = XMLHelper<LanguageConfiguration>.LoadFromFile(this._datafile);
            var entries = languageConfiguration.LanguageEntries;
            var id2Languages = entries.ToDictionary(x => x.ID, x => x.LanguageTexts);

            foreach (var languageEntry in id2Languages)
            {
                var msgId = languageEntry.Key;
                foreach (var languageText in languageEntry.Value)
                {
                    var language = languageText.LanguageCode;
                    var msg = languageText.Value;
                    StringDictionary dict;
                    if (!_languageDictionary.TryGetValue(language, out dict))
                    {
                        dict = new StringDictionary();
                        _languageDictionary[language] = dict;
                    }

                    dict[msgId] = msg;
                }
            }

            _languages = this._languageDictionary.Keys.Select(x => new CultureInfo(x));
        }

        #region Implementation of ITranslationProvider

        /// <summary>
        /// Translates the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public object Translate(string key)
        {
            return this._languageDictionary[CurrentLanguageCode][key];
        }

        #endregion

        #endregion
    }
}